package com.boc.address.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.boc.address.R;
import com.boc.address.R2;
import com.boc.address.bean.AddressListBean;
import com.boc.address.contract.AddressListContract;
import com.jcodecraeer.xrecyclerview.BaseRecyclerAdapter;
import com.jcodecraeer.xrecyclerview.BaseRecyclerViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import demo.bocweb.com.sdk.config.ARoutConfig;

/**
 * 地址列表ada
 * Created by zhelun on 2018/2/2.
 */

public class AddressListAda extends BaseRecyclerAdapter<AddressListBean> {
    private boolean isChoose;
    private AddressListContract.IAddressListView anInterface;

    public AddressListAda(boolean isChoose, AddressListContract.IAddressListView anInterface) {
        this.isChoose = isChoose;
        this.anInterface = anInterface;
    }

    @Override
    public void showData(BaseRecyclerViewHolder viewHolder, final int i, List<AddressListBean> mItemDataList) {
        MyViewHolder holder = (MyViewHolder) viewHolder;
        final AddressListBean bean = mItemDataList.get(i);
        if (isChoose) {
            holder.tvEdit.setVisibility(View.GONE);
            holder.tvDelete.setVisibility(View.GONE);
        } else {
            holder.tvEdit.setVisibility(View.VISIBLE);
            holder.tvDelete.setVisibility(View.VISIBLE);
        }
        if (bean.isIsDefault()) {
            holder.ivDefault.setImageResource(R.mipmap.ic_address_default);
        } else {
            holder.ivDefault.setImageResource(R.mipmap.ic_car_unchoose);
        }
        holder.tvName.setText(bean.getContact());
        holder.tvPhone.setText(bean.getPhone());
        holder.tvAddress.setText(bean.getProvinceName() + bean.getCityName() + bean.getDistrictName() + bean.getAddress());
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anInterface.delete(i);
            }
        });
        holder.ivDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anInterface.setDefault(i);
            }
        });
        holder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutConfig.addAddressPath)
                        .withSerializable("addressBean", bean)
                        .navigation();
            }
        });

    }

    @Override
    public int getListLayoutId() {
        return R.layout.item_address_list;
    }

    @Override
    public RecyclerView.ViewHolder createViewHolder(View view) {
        return new MyViewHolder(view);
    }

    class MyViewHolder extends BaseRecyclerViewHolder {

        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_phone)
        TextView tvPhone;
        @BindView(R2.id.tv_address)
        TextView tvAddress;
        @BindView(R2.id.iv_default)
        ImageView ivDefault;
        @BindView(R2.id.tv_isdefault)
        TextView tvIsdefault;
        @BindView(R2.id.ll_default)
        LinearLayout llDefault;
        @BindView(R2.id.tv_edit)
        TextView tvEdit;
        @BindView(R2.id.tv_delete)
        TextView tvDelete;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
